﻿
using LanqiQAwebapi.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Data;
using LanqiQAwebapi.Interface;

using LanqiQAwebapi.Todo;
using LanqiQAwebapi.Dto;
using static System.Net.Mime.MediaTypeNames;
using LanqiQAwebapi.Filter;
namespace LanqiQAwebapi.Controllers
{

    [ApiController]
    [Route("api/[controller]")]
    [EnableCors("AllowCors")]
    public class LoginController : ControllerBase
    {
        private readonly ILogger<LoginController> _logger;
        private readonly IUserServer _userServer;
        public LoginController(ILogger<LoginController> logger, IUserServer userServer)
        {
            _logger = logger;
            _userServer = userServer;
        }
        [HttpPost("LoginByUser")]
        [EnableCors("AllowCors")]
        [PreventDuplicateRequests(3, "Username", "UserPwd")]
        public async Task<BaseResponse> Login( UserLoginByUser credentials)
        {
            var data = await _userServer.UserLoginByUserAsync(credentials);
            var data1 = data;
          
            return BaseResponse.SetSuccess(data);
        }
        [EnableCors("AllowCors")]
        [HttpPost("TestTaken")]
        //[Authorize(Roles = "r_admin")]
        public async Task<JsonResult> TestTaken(string test)
        {
            JsonResult s = new JsonResult(test);
            return s;
        }
        [EnableCors("AllowCors")]
        [HttpPost("UserAddByPhone")]
        // [Authorize(Roles = "r_admin")]
        public async Task<JsonResult> UserAddByPhone(AddUserByPhone addUserByPhone)
        {
            if (await _userServer.AddUserByPhoneAsync(addUserByPhone))
            {
                JsonResult s = new JsonResult("注册成功");
                return s;
            }
            else
            {
                JsonResult s = new JsonResult("已注册请登录！");
                return s;

            }


        }



        [EnableCors("AllowCors")]
        [HttpPost("loginByPhone")]

        // [Authorize(Roles = "r_admin")]   
        public async Task<JsonResult> LoginByPhone(LoginByPhone addUserByPhone)
        {

           // var DATA = addUserByPhone.Phone + "_System.Threading.Tasks.Task`1[System.Int32]";

            return new JsonResult(await _userServer.UserLoginByphoneAsync(addUserByPhone));

        }
        [EnableCors("AllowCors")]
        [HttpPost("PhoneCode")]
        // [Authorize(Roles = "r_admin")]
        public async Task<JsonResult> PhoneCode(UserLoginByphone loginByPhone)
        {
            return new JsonResult(await _userServer.SendPhoneMMessageCodeAsync(loginByPhone));
        }
        [EnableCors("AllowCors")]
        [HttpPost("AddFollow")]
        // [Authorize(Roles = "r_admin")]
        public BaseResponse<bool> FollowAdd(FollowDto followDto)
        {

          var data=  _userServer.addFollow(followDto.userId, followDto.followId);
            return BaseResponse<bool>.SetSuccess(data);
        }
        [EnableCors("AllowCors")]
        [HttpPost("unFollow")]
        // [Authorize(Roles = "r_admin")]
        public BaseResponse<bool> cancelFollow(FollowDto followDto)
        {

            var data = _userServer.unfollow(followDto.userId, followDto.followId);
            return BaseResponse<bool>.SetSuccess(data);
        }
    }

}
